home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.09 Sep 94 / Programmers' Challenge / RGBtoYUV.c next >
Encoding:
C/C++ Source or Header  |  1994-07-13  |  1.8 KB  |  72 lines  |  [TEXT/KAHL]

  1. /*
  2.     RGBtoYUV.c - Convert RGB color to YUV color.
  3.     
  4.     MacTech Magazine Programmers' Challenge
  5.     July, 1994
  6.     Written by Robert A. Noll
  7.     
  8.     Copyright (c) 1994 Robert A. Noll
  9. */
  10.  
  11. #include "RGBtoYUV.h"
  12.  
  13. /*
  14.     RGBtoYUV - Calculate the Y, U, and V values for set of
  15.     R, G, and B values input.  This function relies
  16.     completely on the data set up by RGBtoYUVInit and
  17.     therefore checks the validity of the data sent.  It
  18.     will return with no action if data is not valid.
  19. */
  20. void RGBtoYUV(unsigned char *rPtr,    /* Red buffer */
  21.               unsigned char *gPtr,    /* Green buffer */
  22.               unsigned char *bPtr,    /* Blue buffer */
  23.               unsigned char *yPtr,    /* Luminance buffer */
  24.               signed char *uPtr,    /* U Chrominance buff */
  25.               signed char *vPtr,    /* V Chrominance buff */
  26.               unsigned long numPixels,/* Number of Pixels */
  27.               void *privateDataPtr)    /* My private data */
  28.     {
  29.     PrivateBlock* pb = (PrivateBlock*)privateDataPtr;
  30.     unsigned long *rp = pb->rp;
  31.     unsigned long *gbp = pb->gbp;
  32.     unsigned char *up = pb->up;
  33.     unsigned char *vp = pb->vp;
  34.     unsigned long tmp;
  35.     union {
  36.         unsigned short i;
  37.         unsigned char c[2];
  38.         } gb;
  39.         
  40.     if (pb->sig != 'RNCS') return;
  41.     
  42.     for (; numPixels>0; --numPixels,++rPtr,++gPtr,++bPtr,
  43.                                     ++yPtr,++uPtr,++vPtr) {
  44.  
  45.         /*    Get index into 'gb' and 'v' tables  */
  46.         gb.c[0] = *gPtr;
  47.         gb.c[1] = *bPtr;
  48.         
  49.         /*    Calculate Y value from tables  */
  50.         tmp = rp[*rPtr] + gbp[gb.i];
  51.         *yPtr = *((unsigned char*)&tmp);
  52.         
  53.         /*  Calculate V value from table  */
  54.         *vPtr = (*rPtr - vp[gb.i]) >> 1;
  55.         
  56.         /*    Get index into 'u' table  */
  57.         gb.c[0] = *rPtr;
  58.         gb.c[1] = *gPtr;
  59.         
  60. #if NegToZero
  61.         /*    If for Negs .5 rounds up to 0 then
  62.          *    if R == G and R|G > B we calc special.
  63.          */
  64.         if (*rPtr==*gPtr && *gPtr>*bPtr)
  65.             *uPtr = ((*bPtr - *gPtr) + 1) >> 1;
  66.         else
  67. #endif
  68.         /*  Calculate U value from table  */
  69.         *uPtr = (*bPtr - up[gb.i]) >> 1;
  70.         }
  71.     }
  72.